﻿using API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Dapper;
using Files.Common;
using Files.Entity;
using System.Web.Script.Serialization;

namespace API.Controllers
{
    public class MatchController : ApiController
    {
        #region [大赛类型]
        [HttpPost, HttpGet, Route("api/Match/queryCompetitionType")]
        public IHttpActionResult queryCompetitionType(string Name, int limit, int page)
        {
            using (IDbConnection db = new SqlConnection(ConfigurationFiles.connectionString))
            {
                StringBuilder sb = new StringBuilder();
                StringBuilder sb1 = new StringBuilder();

                sb1.AppendFormat("select * from ZX_CompetitionType");

                sb.AppendFormat(@" SELECT  A.* FROM  (SELECT TOP {0} * FROM ZX_CompetitionType ORDER BY Id ASC  ) A 
WHERE A.Id NOT IN(SELECT TOP {1} Id FROM ZX_CompetitionType ORDER BY Id ASC)", limit * page, (page - 1) * limit);

                if (!string.IsNullOrEmpty(Name))
                {
                    sb.AppendFormat("  and Name like '%{0}%'", Name);
                    sb1.AppendFormat("  where Name like '%{0}%'", Name);
                }
                List<ZX_CompetitionType> list = db.Query<ZX_CompetitionType>(sb.ToString(), null).ToList();

                var result = from c in list
                             select new
                             {
                                 c.Id,
                                 c.Name,
                                 InsertTime = c.InsertTime.ToString("yyyy-MM-dd"),
                                 UpdateTime = c.UpdateTime.ToString("yyyy-MM-dd")
                             };

                List<ZX_CompetitionType> totalCount = db.Query<ZX_CompetitionType>(sb1.ToString(), null).ToList();

                var jsonResult = new { code = 0, msg = "", count = totalCount.Count, data = result };

                return Json(jsonResult);
            }
        }

        [HttpPost, HttpGet, Route("api/Match/queryCompetitionTypeById")]
        public IHttpActionResult queryCompetitionTypeById(int Id)
        {
            using (IDbConnection db = new SqlConnection(ConfigurationFiles.connectionString))
            {
                StringBuilder sb = new StringBuilder();

                sb.AppendFormat(@"select * from ZX_CompetitionType where Id={0}", Id);
                List<ZX_CompetitionType> list = db.Query<ZX_CompetitionType>(sb.ToString(), null).ToList();
                var result = from c in list
                             select new
                             {
                                 c.Id,
                                 c.Name,
                                 InsertTime = c.InsertTime.ToString("yyyy-MM-dd"),
                                 UpdateTime = c.UpdateTime.ToString("yyyy-MM-dd")
                             };

                var jsonResult = new { data = result };

                return Json(jsonResult);
            }
        }

        [HttpPost, HttpGet, Route("api/Match/editCompetitionTypeById")]
        public IHttpActionResult editCompetitionTypeById(int Id, string Name)
        {
            using (IDbConnection db = new SqlConnection(ConfigurationFiles.connectionString))
            {
                StringBuilder sb = new StringBuilder();
                Message ms = new Message();
                if (Id == 0)
                {
                    sb.AppendFormat(@"insert into ZX_CompetitionType values('{0}', '{1}', '{2}')", Name, DateTime.Now, DateTime.Now);
                }
                else
                {
                    sb.AppendFormat(@"update ZX_CompetitionType set Name='{0}',UpdateTime='{1}'  where Id={2}", Name, DateTime.Now, Id);
                }
                int count = db.Execute(sb.ToString());
                if (count > 0)
                {
                    ms.success = true;
                    ms.code = "1111";
                    ms.msg = "操作成功";
                    ms.data = "";
                }
                else
                {
                    ms.success = false;
                    ms.code = "0000";
                    ms.msg = "操作失败";
                    ms.data = "";
                }
                return Json(new JavaScriptSerializer().Serialize(ms));
            }
        }

        [HttpPost, HttpGet, Route("api/Match/removeCompetitionTypeById")]
        public IHttpActionResult removeCompetitionTypeById(int Id)
        {
            using (IDbConnection db = new SqlConnection(ConfigurationFiles.connectionString))
            {
                StringBuilder sb = new StringBuilder();
                Message ms = new Message();
                sb.AppendFormat(@"delete from ZX_CompetitionType  where Id={0}", Id);
                int count = db.Execute(sb.ToString());
                if (count > 0)
                {
                    ms.success = true;
                    ms.code = "1111";
                    ms.msg = "删除成功";
                    ms.data = "";
                }
                else
                {
                    ms.success = false;
                    ms.code = "0000";
                    ms.msg = "删除失败";
                    ms.data = "";
                }
                return Json(new JavaScriptSerializer().Serialize(ms));
            }
        }

        #endregion

        #region[赛项]

        [HttpPost, HttpGet, Route("api/Match/queryEvents")]
        public IHttpActionResult queryEvents(string Name, int limit, int page)
        {
            string Name1 = Request.Content.ReadAsStringAsync().Result;

            using (IDbConnection db = new SqlConnection(ConfigurationFiles.connectionString))
            {
                StringBuilder sb = new StringBuilder();
                StringBuilder sb1 = new StringBuilder();
                sb1.AppendFormat("select * from ZX_Events");

                sb.AppendFormat(@" SELECT  A.* FROM  (SELECT TOP {0} * FROM ZX_Events ORDER BY Id ASC  ) A 
WHERE A.Id NOT IN(SELECT TOP {1} Id FROM ZX_Events ORDER BY Id ASC)", limit * page, (page - 1) * limit);

                if (!string.IsNullOrEmpty(Name))
                {
                    sb.AppendFormat("  and Name like '%{0}%'", Name);
                    sb1.AppendFormat("  where Name like '%{0}%'", Name);
                }
                List<ZX_Events> list = db.Query<ZX_Events>(sb.ToString(), null).ToList();

                var result = from c in list
                             select new
                             {
                                 c.Id,
                                 c.Name,
                                 c.CompetitionTypeId,
                                 InsertTime = c.InsertTime.ToString("yyyy-MM-dd"),
                                 UpdateTime = c.UpdateTime.ToString("yyyy-MM-dd")
                             };
                List<ZX_Events> totalCount = db.Query<ZX_Events>(sb1.ToString(), null).ToList();
                var jsonResult = new { code = 0, msg = "", count = totalCount.Count, data = result };

                return Json(jsonResult);
            }
        }
        /// <summary>
        /// 删除赛项
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("api/Match/removeEventsById")]
        public IHttpActionResult removeEventsById(int Id)
        {
            using (IDbConnection db = new SqlConnection(ConfigurationFiles.connectionString))
            {
                StringBuilder sb = new StringBuilder();
                Message ms = new Message();
                sb.AppendFormat(@"delete from ZX_Events  where Id={0}", Id);
                int count = db.Execute(sb.ToString());
                if (count > 0)
                {
                    ms.success = true;
                    ms.code = "1111";
                    ms.msg = "删除成功";
                    ms.data = "";
                }
                else
                {
                    ms.success = false;
                    ms.code = "0000";
                    ms.msg = "删除失败";
                    ms.data = "";
                }
                return Json(new JavaScriptSerializer().Serialize(ms));
            }
        }

        /// <summary>
        /// 添加/编辑赛项
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Cid"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("api/Match/editEventsById")]
        public IHttpActionResult editEventsById(int Id, int Cid, string Name)
        {
            using (IDbConnection db = new SqlConnection(ConfigurationFiles.connectionString))
            {
                StringBuilder sb = new StringBuilder();
                Message ms = new Message();
                if (Id == 0)
                {
                    sb.AppendFormat(@"insert into ZX_Events values({0}, '{1}', '{2}','{2}')", Cid, Name, DateTime.Now, DateTime.Now);
                }
                else
                {
                    sb.AppendFormat(@"update ZX_Events set Name='{0}',CompetitionTypeId={1} ,UpdateTime='{2}'  where Id={3}", Name,Cid, DateTime.Now, Id);
                }
                int count = db.Execute(sb.ToString());
                if (count > 0)
                {
                    ms.success = true;
                    ms.code = "1111";
                    ms.msg = "操作成功";
                    ms.data = "";
                }
                else
                {
                    ms.success = false;
                    ms.code = "0000";
                    ms.msg = "操作失败";
                    ms.data = "";
                }
                return Json(new JavaScriptSerializer().Serialize(ms));
            }
        }

        #endregion

        #region[参赛人员类型]

        [HttpPost, HttpGet, Route("api/Match/queryParticipantsType")]
        public IHttpActionResult queryParticipantsType(string Name, int limit, int page)
        {

            using (IDbConnection db = new SqlConnection(ConfigurationFiles.connectionString))
            {
                StringBuilder sb = new StringBuilder();

                StringBuilder sb1 = new StringBuilder();
                sb1.Append("select * from ZX_ParticipantsType");


                sb.AppendFormat(@" SELECT  A.* FROM  (SELECT TOP {0} * FROM ZX_ParticipantsType ORDER BY Id ASC  ) A 
WHERE A.Id NOT IN(SELECT TOP {1} Id FROM ZX_ParticipantsType ORDER BY Id ASC)", limit * page, (page - 1) * limit);

                if (!string.IsNullOrEmpty(Name))
                {
                    sb.AppendFormat("  and Name like '%{0}%'", Name);
                    sb1.AppendFormat("  where Name like '%{0}%'", Name);
                }
                List<ZX_ParticipantsType> list = db.Query<ZX_ParticipantsType>(sb.ToString(), null).ToList();

                List<ZX_ParticipantsType> totalCount = db.Query<ZX_ParticipantsType>(sb1.ToString(), null).ToList();
                var result = from c in list
                             select new
                             {
                                 c.Id,
                                 c.Name,
                                 InsertTime = c.InsertTime.ToString("yyyy-MM-dd"),
                                 UpdateTime = c.UpdateTime.ToString("yyyy-MM-dd")
                             };

                var jsonResult = new { code = 0, msg = "", count = totalCount.Count, data = result };

                return Json(jsonResult);
            }
        }

        /// <summary>
        /// 添加/删除人员类型
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("api/Match/editParticipantsTypeById")]
        public IHttpActionResult editParticipantsTypeById(int Id, string Name)
        {
            using (IDbConnection db = new SqlConnection(ConfigurationFiles.connectionString))
            {
                StringBuilder sb = new StringBuilder();
                Message ms = new Message();
                if (Id == 0)
                {
                    sb.AppendFormat(@"insert into ZX_ParticipantsType values('{0}', '{1}', '{2}',)",  Name, DateTime.Now, DateTime.Now);
                }
                else
                {
                    sb.AppendFormat(@"update ZX_ParticipantsType set Name='{0}' ,UpdateTime='{1}'  where Id={2}", Name, DateTime.Now, Id);
                }
                int count = db.Execute(sb.ToString());
                if (count > 0)
                {
                    ms.success = true;
                    ms.code = "1111";
                    ms.msg = "操作成功";
                    ms.data = "";
                }
                else
                {
                    ms.success = false;
                    ms.code = "0000";
                    ms.msg = "操作失败";
                    ms.data = "";
                }
                return Json(new JavaScriptSerializer().Serialize(ms));
            }
        }

        /// <summary>
        /// 删除人员类型
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("api/Match/removeParticipantsTypeById")]
        public IHttpActionResult removeParticipantsTypeById(int Id)
        {
            using (IDbConnection db = new SqlConnection(ConfigurationFiles.connectionString))
            {
                StringBuilder sb = new StringBuilder();
                Message ms = new Message();
                sb.AppendFormat(@"delete from ZX_ParticipantsType  where Id={0}", Id);
                int count = db.Execute(sb.ToString());
                if (count > 0)
                {
                    ms.success = true;
                    ms.code = "1111";
                    ms.msg = "删除成功";
                    ms.data = "";
                }
                else
                {
                    ms.success = false;
                    ms.code = "0000";
                    ms.msg = "删除失败";
                    ms.data = "";
                }
                return Json(new JavaScriptSerializer().Serialize(ms));
            }
        }

        #endregion

    }
}
